package com.pachong.android.frameworkbase.utils;



import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期时间工具
 * 1. 获取某个日期的格式化的字符串
 * 2. 自定义格式
 * 3. 将格式化的字符串解析成Date
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-06-20
 * @Version: 1.0.0
 */
public class DateTimeUtil {

	public static final String DATE_FORMAT_DEF = "yyyy-MM-dd";
	public static final String TIME_FORMAT_DEF = "HH:mm";

	public static final String DATETIME_FORMAT_DEF = DATE_FORMAT_DEF + " " + TIME_FORMAT_DEF;

	/**
	 * G Era 标志符 Text AD
	 * y 年 Year 1996; 96
	 * M 年中的月份 Month July; Jul; 07
	 * w 年中的周数 Number 27
	 * W 月份中的周数 Number 2
	 * D 年中的天数 Number 189
	 * d 月份中的天数 Number 10
	 * F 月份中的星期 Number 2
	 * E 星期中的天数 Text Tuesday; Tue
	 * a Am/pm 标记 Text PM
	 * H 一天中的小时数（0-23） Number 0
	 * k 一天中的小时数（1-24） Number 24
	 * K am/pm 中的小时数（0-11） Number 0
	 * h am/pm 中的小时数（1-12） Number 12
	 * m 小时中的分钟数 Number 30
	 * s 分钟中的秒数 Number 55
	 * S 毫秒数 Number 978
	 * z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
	 * Z 时区 RFC 822 time zone -0800
	 */
	public DateTimeUtil() {
		// TODO Auto-generated constructor stub
	}


	public static String getCurrDateStr() {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DEF, Locale.CHINA);
		return format.format(new Date());
	}


	public static String getCurrTimeStr() {
		SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT_DEF, Locale.CHINA);
		return format.format(new Date());
	}


	public static String getCurrDateTimeStr() {
		SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMAT_DEF, Locale.CHINA);
		return format.format(new Date());
	}


	public static String getDateStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DEF, Locale.CHINA);
		return format.format(date);
	}


	public static String getTimeStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT_DEF, Locale.CHINA);
		return format.format(date);
	}


	public static String getTimeStr(Date date, String formater) {
		SimpleDateFormat format = new SimpleDateFormat(formater, Locale.CHINA);
		return format.format(date);
	}


	public static String getDateTimeStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMAT_DEF, Locale.CHINA);
		return format.format(date);
	}


	public static String getDateTimeStr(Date date, String formater) {
		SimpleDateFormat format = new SimpleDateFormat(formater, Locale.CHINA);
		return format.format(date);
	}


	/**
	 * yyyy-MM-dd HH:mm default
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateTimeStr(String dateStr, String timeStr, String formatter) {
		if(TextUtils.isEmpty(formatter)) {
			formatter = DATETIME_FORMAT_DEF;
		}
		SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMAT_DEF, Locale.CHINA);
		Date date = null;
		try {
			date = format.parse(dateStr + " " + timeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}


	/**
	 * yyyy-MM-dd default
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateStr(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DEF, Locale.CHINA);
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}


	/**
	 * HH:mm default
	 * 
	 * @param timeStr
	 * @return
	 */
	public static Date parseTimeStr(String timeStr) {
		SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT_DEF);
		Date date = null;
		try {
			date = format.parse(timeStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}


	/**
	 * 判断某个日期是星期几
	 * 
	 * @return
	 */
	public static String getWeekDayStr(Date date) {
		final String week[] = { "天", "一", "二", "三", "四", "五", "六" };
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int i = c.get(Calendar.DAY_OF_WEEK);
		return "星期" + week[i - 1];
	}


	public static void showTimeSettingDialog(final Context context, Date date,
			final OnTimeSetListener listener) {
		Calendar cal = Calendar.getInstance();
		if (date != null) {
			cal.setTime(date);
		}
		TimePickerDialog timePickerDialog = new TimePickerDialog(context, listener,
				cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true);
		timePickerDialog.show();

	}


	public static void showDateSettingDialog(Context context, Date date, final OnDateSetListener listener) {
		Calendar cal = Calendar.getInstance();
		if (date != null) {
			cal.setTime(date);
		}
		DatePickerDialog datePickerDialog = new DatePickerDialog(context, listener,
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH));

		datePickerDialog.show();

	}


	public static String getYearMonthStr(Date date, String formatStr) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
		return format.format(date);
	}


	/**
	 * 获取某个日期的格式化字符串
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String getDateStrOf(Date date, String formatStr) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
		return format.format(date);
	}


	/**
	 * 判断所给的月份有多少天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getMonthDayOf(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);// Java月份从0开始算
		int dateOfMonth = cal.getActualMaximum(Calendar.DATE);

		return dateOfMonth;
	}
	
	
	/**
	 * 判断是不是今天
	 * 
	 * @param time
	 * @return
	 */
	public static boolean isToday(long time) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		long todayStart = cal.getTimeInMillis();
		long oneDay = 24 * 60 * 60 * 1000L;
		long todayEnd = todayStart + oneDay;

		return todayStart <= time && time < todayEnd;
	}
	
	public static Date parseDateTimeStr(String dateTimeStr, String formatStr){
		SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
		Date date;
		try {
			date = format.parse(dateTimeStr);
		} catch (ParseException e) {
			date = new Date(0);
			e.printStackTrace();
		}
		return date;
	}

	/*df.applyPattern("yyyy'年'M'月'd'日'\nh'时'm'分'mm'秒'");  
	System.out.println("更为自由的格式，嵌入字符、换行等:\n"   
	        + df.format(dNow) + "\n\n\n");  */
}
